////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.users.windows.objects {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	import kiwi.components.Button;
	import kiwi.events.CallbackEvent;
	import kiwi.utils.Validation;
	
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.display.windows.BaseWindow;
	import plantool.display.windows.objects.WindowRow;
	import plantool.helpers.TextFieldHelper;
	import plantool.modules.users.data.User;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class UserListWindowUserRow extends WindowRow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _user:User		= null;
		private var _tf:TextField	= null;
		private var _editBtn:Button	= null;
		private var _delBtn:Button	= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get user():User 			{ return _user; }
		public function set user(value:User):void 	{ _user = value; invalidate(); }
		
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function UserListWindowUserRow(window:BaseWindow) {
			super(window);
		}
		
		public override function dispose():void {
			//unset user
			_user 	= null;
			
			//remove textfield
			if (_tf) {
				if (contains(_tf)) {
					removeChild(_tf);
				}
				_tf		= null;
			}
			
			//dispose buttons
			if (_editBtn) {
				_editBtn.dispose();
				_editBtn = null;
			}
			if (_delBtn) {
				_delBtn.dispose();
				_delBtn = null;
			}
			
			//super dispose
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onComplete_removeUser(e:QueryResult):void {
		}
		
		private function onError_removeUser(e:QueryErrorResult):void {
			trace('error', e.message);
		}
		
		private function mouseHandler_delBtn(e:CallbackEvent):void {
			switch (e.userData.type) {
				case MouseEvent.CLICK:
					userApi.removeUser(_user, onComplete_removeUser, onError_removeUser);
					break;
			}
		}
		
		protected override function init():void {
			//create label
			_tf 	= TextFieldHelper.createInfoTf(TextFieldAutoSize.LEFT, TextFormatAlign.LEFT, false, false, 0);
			addChild(_tf);
			
			//create buttons
			_editBtn 		= new Button();
			_editBtn.width 	= 10;
			_editBtn.height = 10;
			_editBtn.x		= width - 30;
			_editBtn.y		= (height - 10) / 2;
			addChild(_editBtn);
			
			_delBtn 		= new Button();
			_delBtn.width 	= 10;
			_delBtn.height 	= 10;
			_delBtn.x		= width - 15;
			_delBtn.y		= (height - 10) / 2;
			_delBtn.mouseHandler = mouseHandler_delBtn;
			addChild(_delBtn);
			
			super.init();
		}
		
		public override function draw():void {
			//update textfield
			if (Validation.isSet(_tf)) {
				//textfield position
				_tf.x 	= 8;
				_tf.y	= (height - _tf.height) / 2;
				
				//show username
				if (Validation.isSet(_user)) {
					_tf.text = _user.username;
				}
			}
			
			//super draw
			super.draw();
		}
	}
}